/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = rtcp_comm.h                                        */
/*  NOTE      =                                                    */
/*  DATE      = 2015/07/20 by songbb                               */
/*******************************************************************/

#ifndef __RTCP_COMM_H_
#define __RTCP_COMM_H_

#include <sys/time.h>

#include "frlog.h"

#include "rtcp_core.h"

#define APM_RTCP_DBG 0
#define APM_RTCP_ENABLE_FRLOG 0

/* 标识符变量的初始化值 */
#define APM_RTCP_FLAG_UNSET -1

/* Version is the first 2 bits of the first octet*/
#define APM_RTCP_VERSION(octet) ((octet) >> 6)

/* Padding is the third bit; no need to shift, because true is any value
   other than 0! */
#define APM_RTCP_PADDING(octet) ((octet) & 0x20)

/* Receiver/ Sender count is the 5 last bits  */
#define APM_RTCP_COUNT(octet)   ((octet) & 0x1F)

#define APM_RTCP_PKT_PT_OFFSET 1
#define APM_RTCP_PKT_LEN_OFFSET 2

#define APM_RTCP_PT_MIN  192
#define APM_RTCP_PT_MAX  210

#define APM_RTCP_REPORT_MODULE_NAME "rtcp"
#define APM_RTCP_REPORT_KEY APM_REPORT_KEY_REQ"."APM_RTCP_REPORT_MODULE_NAME".compound"

#define KRED        "\x1B[0;31m"
#define KSALMON     "\x1B[1;31m"
#define KGREEN      "\x1B[0;32m"
#define KLIMEGREEN  "\x1B[1;32m"
#define KYELLOW     "\x1B[0;33m"
#define KORANGE     "\x1B[0;34m"
#define KBLUE       "\x1B[1;34m"
#define KMAGENTA    "\x1B[0;35m"
#define KPINK       "\x1B[1;35m"
#define KCYAN       "\x1B[0;36m"
#define RESET       "\x1B[0m"

#if APM_RTCP_DBG
    #define RTCP_PRINT(...) printf(__VA_ARGS__)
    #define RTCP_PRINT_DETAIL(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(__VA_ARGS__))
    #define RTCP_PRINT_DBG(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KGREEN),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define RTCP_PRINT_FATAL(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KORANGE),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define RTCP_PRINT_EMERG(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KRED),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define RTCP_PRINT_ALERT(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KSALMON),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define RTCP_PRINT_CRIT(...) (printf("[%s, Line:%u, %s()]",\
                            __FILE__,__LINE__,__func__),\
                            printf(KMAGENTA),\
                            printf(" "__VA_ARGS__),\
                            printf(RESET))
    #define RTCP_PRINT_ERR(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KPINK),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define RTCP_PRINT_WARN(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KYELLOW),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define RTCP_PRINT_NOTICE(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KLIMEGREEN),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define RTCP_PRINT_INFO(...) (printf("[%s, Line:%u, %s()]",\
                            __FILE__,__LINE__,__func__),\
                            printf(KBLUE),\
                            printf(" "__VA_ARGS__),\
                            printf(RESET))
     #define PRINT_HEX_STRING(a,b,c) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KCYAN),\
                            printf(a":["),rtcp_print_hex_string(b,c),printf("]\n"),\
                            printf(RESET))
    #define RTCP_PRINT_PACKET_SPECIFIED(tag, packet, offset, len) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KORANGE),\
                            printf(tag":["),rtcp_print_packet_specified(packet,offset,len),printf("]\n"),\
                            printf(RESET))

    #define RTCP_PRINT_PACKET_CURRENT(a,b) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KCYAN),\
                            printf(a":["),rtcp_print_packet_current(b),printf("]\n"),\
                            printf(RESET))

    #define RTCP_PRINT_PACKET(a,b) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KCYAN),\
                            printf(a":["),rtcp_print_packet(b),printf("]\n"),\
                            printf(RESET))
    #define RTCP_PRINT_TIMEVAL(a,b) (printf("[%s, Line:%u, %s()]",\
                            __FILE__,__LINE__,__func__),\
                            printf(KGREEN),\
                            printf(" %s", a), \
                             rtcp_print_timeval(b),\
                             printf(RESET))
#else
    #define RTCP_PRINT(...)
    #define RTCP_PRINT_DETAIL(...)
    #define RTCP_PRINT_DBG(...)
    #define RTCP_PRINT_FATAL(...)
    #define RTCP_PRINT_EMERG(...)
    #define RTCP_PRINT_ALERT(...)
    #define RTCP_PRINT_CRIT(...)
    #define RTCP_PRINT_ERR(...)
    #define RTCP_PRINT_WARN(...)
    #define RTCP_PRINT_NOTICE(...)
    #define RTCP_PRINT_INFO(...)
    #define PRINT_HEX_STRING(a,b,c)
    #define RTCP_PRINT_PACKET_SPECIFIED(tag, packet, offset, len)
    #define RTCP_PRINT_PACKET_CURRENT(a,b)
    #define RTCP_PRINT_PACKET(a,b)
    #define RTCP_PRINT_TIMEVAL(a,b)
#endif

#if APM_RTCP_ENABLE_FRLOG
    #define RTCP_FRLOG_ERROR(...) FRLOG_ERROR_DETAIL(...)
#else
#define RTCP_FRLOG_ERROR(...)
#endif

#define RTCP_MALLOCK_ERR RTCP_PRINT_EMERG("can not malloc memory\n"); \
                          FRLOG_ERROR_DETAIL("can not malloc memory");

void rtcp_free(void **pp_buffer);

void rtcp_print_timeval(struct timeval st_timestamp);

void rtcp_util_to_hex_string(char *p_inbuff
                            , int i_len
                            , char *p_outbuff);

void rtcp_print_hex_string(char *p_buff
                        , int i_len);

void rtcp_print_packet_specified(L7_rtcp_packet *p_st_rtcp_packet
                                , int i_pos
                                , int i_len);

void rtcp_print_packet_current(L7_rtcp_packet *p_st_rtcp_packet);

void rtcp_print_packet(L7_rtcp_packet *p_st_rtcp_packet);

#endif
